package com.zero.base;

import com.zero.LogTag;
import com.zero.PublicResult;
import io.swagger.annotations.ApiOperation;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.validation.Valid;
import java.util.Collection;

/**
 * 控制层基类
 */
@NoArgsConstructor
public class BaseController<E extends BaseEntity, ID, B extends IBase<E, ID, Q>, Q extends BaseQuery> {
    @Autowired
    private B base;

    @GetMapping("get")
    @ApiOperation(value = "获取一条", notes = "通过主键获取一条信息")
    public PublicResult get(@RequestParam("id") ID id) {
        return base.get(id);
    }

    @PostMapping("list")
    @ApiOperation(value = "列表", notes = "查询数据集合")
    public PublicResult list(@RequestBody @Valid Q query) {
        return base.list(query);
    }

    @PostMapping("pageList")
    @ApiOperation(value = "分页列表", notes = "查询分页数据集合")
    public PublicResult pageList(@RequestBody @Valid Q query) {
        return base.pageList(query);
    }

    @GetMapping("del")
    @ApiOperation(value = "删除", notes = "公用删除接口")
    public PublicResult deleteByIds(@RequestParam("ids") Collection<ID> ids) {
        return base.deleteByIds(ids);
    }

    @PostMapping("add")
    @LogTag
    @ApiOperation(value = "新增", notes = "公用新增接口")
    public PublicResult save(@RequestBody @Valid E entity) {
        return base.add(entity);
    }

    @PostMapping("update")
    @ApiOperation(value = "修改", notes = "公用修改接口")
    public PublicResult update(@RequestBody @Valid E entity) {
        return base.update(entity);
    }

}
